//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.ServiceModel.Client
{
    using System.Collections.Generic;
    using System.Xml.Linq;

    public sealed class EndpointDescription
    {
        private string type;

        public EndpointDescription()
        {
            this.Properties = new Dictionary<string, string>();
        }

        public string Name
        {
            get;
            set;
        }

        public string Type
        {
            get
            {
                return this.type;
            }

            set
            {
                this.type = value != null ? value.Trim().ToLowerInvariant() : null;
            }
        }

        public Dictionary<string, string> Properties
        {
            get;
            private set;
        }

        public XElement ToXml()
        {
            var element = new XElement(
                "endpoint",
                new XAttribute("name", this.Name),
                new XAttribute("type", this.Type));

            foreach (var key in this.Properties.Keys)
            {
                element.Add(
                    new XElement(
                    "property",
                    new XAttribute("name", key),
                    new XAttribute("value", this.Properties[key])));
            }

            return element;
        }

        public void Hydrate(XElement element)
        {
            this.Name = element.Attribute("name").Value;
            this.Type = element.Attribute("type").Value;
            foreach (var child in element.Elements("property"))
            {
                var propertyName = child.Attribute("name").Value;
                var value = child.Attribute("value").Value;
                this.Properties[propertyName] = value;
            }
        }

        public static EndpointDescription CreateFromXml(XElement element)
        {
            var description = new EndpointDescription();
            description.Hydrate(element);
            return description;
        }
    }
}
